package com.cw.frame.codegen;

import java.util.List;

import org.springframework.util.StringUtils;

import com.cw.frame.codegen.parameter.ParamBundle;
import com.cw.frame.codegen.parameter.ParamContext;
import com.cw.jeeyt.common.enums.EnumParamType;
import com.cw.jeeyt.common.enums.EnumTemplateType;
import com.cw.jeeyt.service.bo.system.SystemTemplateModuleInfoBo;

/**
 * To generate the java source file.
 *
 * @author chenw
 */
public class JavaFileGenerator extends AbstractSourceFileGenerator {
    @SuppressWarnings("unchecked")
    @Override
    public void generateConcreteFiles(ParamContext paramContext) throws Exception {

        String outputParentDir = paramContext.getJavaOutputPath();
        String generateType = (String) paramContext.getAttributes()
            .get("generateType");
        List<SystemTemplateModuleInfoBo> stmibList = (List<SystemTemplateModuleInfoBo>) paramContext.getAttributes()
            .get("systemTemplateModuleInfoList");
        for (ParamBundle paramBundle : paramContext.getParamBundles()) {
            paramBundle.setGenerateType(generateType);
            if (paramBundle.getParamType() == EnumParamType.CODE) {
                if (paramBundle.getFieldList() == null || paramBundle.getFieldList()
                    .isEmpty()) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("Ignore this param bundle since no field list found.");
                    }
                    continue;
                }
                //生成代码
                if (stmibList != null && !stmibList.isEmpty()) {
                    for (SystemTemplateModuleInfoBo stmib : stmibList) {
                        if (EnumTemplateType.JAVA.getCode()
                            .equals(stmib.getTemplateModuleType()) && !stmib.getTemplateModuleCode()
                            .equals("Enum")) {
                            createSingleFile(outputParentDir, paramBundle, stmib.getTemplateModuleName(),
                                stmib.getOutputFolder());
                        }
                    }
                }
            } else {
                //生成枚举代码
                if (stmibList != null && !stmibList.isEmpty()) {
                    for (SystemTemplateModuleInfoBo stmib : stmibList) {
                        if (EnumTemplateType.JAVA.getCode()
                            .equals(stmib.getTemplateModuleType()) && stmib.getTemplateModuleCode()
                            .equals("Enum")) {
                            createSingleFile(outputParentDir, paramBundle, stmib.getTemplateModuleName(),
                                stmib.getOutputFolder());
                        }
                    }
                }
            }
            if (logger.isDebugEnabled()) {
                logger.debug("To create java source for param bundle: " + paramBundle);
            }
            if (logger.isDebugEnabled()) {
                logger.debug("Finished to create java source at directory: " + outputParentDir);
            }
        }
    }

    private void createSingleFile(String outputParentDir, ParamBundle paramBundle, String templateName,
        String templateOutputFolder) throws Exception {
        String fileNamePrefix = StringUtils.capitalize(paramBundle.getModuleName());
        String projectType = paramBundle.getProjectType();
        if("go".equalsIgnoreCase(projectType)){
            fileNamePrefix = paramBundle.getTableName();
        }
        String filePath = FileHelper.getFilePath(outputParentDir + "/" + Constant.JAVA_OUTPUT_PATH + "/",
            paramBundle.getPackageName() + "." + paramBundle.getModuleId());
        templateEngine.process(paramBundle, templateName,
            filePath + templateOutputFolder + "/" + templateName.replaceAll(Constant.PRE_CODE_TEMPLATES,
                fileNamePrefix));
    }
}